//----------------------------------------------------------------------------
//  ,-.
// /_\___ ,   Ninja Framework
// '  ___\    (C) Riccardo Capra - http://riccar.do
//  //   \\
//----------------------------------------------------------------------------
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//----------------------------------------------------------------------------

#ifndef NJ_SMART_PTR_INLINE_H
#	define NJ_SMART_PTR_INLINE_H

	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType>::NjSmartPtr()
	:
		m_pPtr(NJ_NULL)
	{
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType>::NjSmartPtr(Type * pPtr)
	:
		m_pPtr(pPtr)
	{
		IncrementReference(pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType>::NjSmartPtr(NjSmartPtr const & kSmartPtr)
	:
		m_pPtr(kSmartPtr.m_pPtr)
	{
		IncrementReference(m_pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType>::~NjSmartPtr()
	{
		DecrementReference(m_pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType> & NjSmartPtr<tType>::operator=(Type * pPtr)
	{
		IncrementReference(pPtr);
		Type * const pPtr2(m_pPtr);
		m_pPtr=pPtr;
		DecrementReference(pPtr2);

		return (*this);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType> & NjSmartPtr<tType>::operator=(NjSmartPtr const & kSmartPtr)
	{
		return operator=(kSmartPtr.m_pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::operator==(Type * pPtr) const
	{
		return m_pPtr==pPtr;
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::operator==(NjSmartPtr const & kSmartPtr) const
	{
		return operator==(kSmartPtr.m_pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::operator!=(Type * pPtr) const
	{
		return !operator==(pPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::operator!=(NjSmartPtr const & kSmartPtr) const
	{
		return !operator==(kSmartPtr);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::operator!() const
	{
		return IsNull();
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjBool NjSmartPtr<tType>::IsNull() const
	{
		return operator==(NJ_NULL);
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline typename NjSmartPtr<tType>::Type & NjSmartPtr<tType>::operator*() const
	{
		NJ_ASSERT(m_pPtr!=NJ_NULL);
		return *m_pPtr;
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline typename NjSmartPtr<tType>::Type * NjSmartPtr<tType>::operator->() const
	{
		NJ_ASSERT(m_pPtr!=NJ_NULL);
		return m_pPtr;
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline NjSmartPtr<tType>::operator typename NjSmartPtr<tType>::Type * () const
	{
		return m_pPtr;
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline void NjSmartPtr<tType>::Swap(NjSmartPtr & kOther)
	{
		if ((&kOther)==this)
			return;

		Type * const pTmpPtr(m_pPtr);
		m_pPtr=kOther.m_pPtr;
		kOther.m_pPtr=pTmpPtr;
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline void NjSmartPtr<tType>::IncrementReference(Type * pPtr)
	{
		if (pPtr==NJ_NULL)
			return;

		pPtr->IncrementReference();
	}
	//----------------------------------------------------------------------------
	template<class tType>
	inline void NjSmartPtr<tType>::DecrementReference(Type * pPtr)
	{
		if (pPtr==NJ_NULL)
			return;

		pPtr->DecrementReference();
	}
	//----------------------------------------------------------------------------

#endif // NJ_SMART_PTR_INLINE_H
